<?php

switch($_POST["txEnt"]){

  case _CASH_BANK_INVOICE_NAME:

    $newEntry = sizeof($_POST["invoice"]);
    break;

  case _CASH_BANK_DP_NAME:

    $newEntry = sizeof($_POST["invoice"]);
    break;

  case _CASH_BANK_CCL_DP_NAME:

    $newEntry = sizeof($_POST["invoice"]);
    break;

  default:
    $Misc->usrDieJs("Error: Unknown Tx");
}


$_SESSION["init_sub"] += $newEntry;

/**
 * fetch the detail
 * and foreach fetched data
 * concat at the end of current rows of data
 */
for($iRow = 0; $iRow < $newEntry ; $iRow++){

  switch($_POST["typeEnt"]){

    case _CASH_BANK_IN:

      switch($_POST["txEnt"]){

        case _CASH_BANK_DP_NAME:

          $CLFetchers->fetchOutstandingRemain2($Db,
                                               "SOURCE",
                                               $_GET["whoseCode"],
                                               $curPeriode,
                                               $_POST["invoice"][$iRow],
                                               $SourceDb->fetchValue("srcDbMutDp"),
                                               $nRemain,
                                               $resRemain);

          $CLFetchers->fetchSourceHeader($Db,
                                         $_POST["invoice"][$iRow],
                                         $SourceDb->fetchValue("srcDbSourceHdr"),
                                         $nHeader,
                                         $resHeader);

          /**
           * process the gain or loss
           */
          $todayCurs   = $_GET["currency"];
          $todayRate   = $_GET["rate"];

          $thatDayCurs = $resHeader["CURRENCY"][0];
          $thatDayRate = $resHeader["RATE"][0];

          if($thatDayCurs == $todayCurs){

            $gainLoss = $todayRate - $thatDayRate;
          }else{

            /* Pending */
            $gainLoss = 0;
          }

          /**
           * get current nRows of input data
           * it could be any valid input name
           */
          $curIndex = sizeof($_POST["trDateEnt"]);

          $_POST["notaSrcEnt"][$curIndex]  = $resHeader["REFERENCE"][0];

          $_POST["currencyEnt"][$curIndex] = $resHeader["CURRENCY"][0];

          $_POST["rateEnt"][$curIndex]     = $resHeader["RATE"][0];

          $_POST["trDateEnt"][$curIndex]   = date("d-m-Y",
                                                  strtotime($resRemain["TR_DATE"][0]));
          $_POST["dueDateEnt"][$curIndex]  = date("d-m-Y",
                                                  strtotime($resRemain["DUE_DATE"][0]));

          $_POST["remainEnt"][$curIndex]   = $resRemain["REMAIN"][0];

          $_POST["receiveEnt"][$curIndex]  = $resRemain["REMAIN"][0];

          $_POST["gainLossEnt"][$curIndex] = $gainLoss;
          break;

        default:
          $Misc->usrDieJs("Error: Unknown Tx");
      }
      break;

    case _CASH_BANK_OUT:

      switch($_POST["txEnt"]){

        case _CASH_BANK_DP_NAME:

          $CLFetchers->fetchOutstandingRemain2($Db,
                                               "SOURCE",
                                               $_GET["whoseCode"],
                                               $curPeriode,
                                               $_POST["invoice"][$iRow],
                                               $SourceDb->fetchValue("srcDbMutDp"),
                                               $nRemain,
                                               $resRemain);

          $CLFetchers->fetchSourceHeader($Db,
                                         $_POST["invoice"][$iRow],
                                         $SourceDb->fetchValue("srcDbSourceHdr"),
                                         $nHeader,
                                         $resHeader);

          /**
           * @todo what if ref has different curs? and is CCL DP applied?
           *
           * process the gain or loss
           */
          $todayCurs   = $_GET["currency"];
          $todayRate   = $_GET["rate"];

          $thatDayCurs = $resHeader["CURRENCY"][0];
          $thatDayRate = $resHeader["RATE"][0];

          if($thatDayCurs == $todayCurs){

            $gainLoss = $thatDayRate - $todayRate;
          }else{

            /* Pending */
            $gainLoss = 0;
          }

          /**
           * get current nRows of input data
           * it could be any valid input name
           */
          $curIndex = sizeof($_POST["trDateEnt"]);

          $_POST["notaSrcEnt"][$curIndex]  = $resHeader["REFERENCE"][0];

          $_POST["currencyEnt"][$curIndex] = $resHeader["CURRENCY"][0];

          $_POST["rateEnt"][$curIndex]     = $resHeader["RATE"][0];

          $_POST["trDateEnt"][$curIndex]   = date("d-m-Y",
                                                  strtotime($resRemain["TR_DATE"][0]));
          $_POST["dueDateEnt"][$curIndex]  = date("d-m-Y",
                                                  strtotime($resRemain["DUE_DATE"][0]));

          $_POST["remainEnt"][$curIndex]   = $resRemain["REMAIN"][0];

          $_POST["paymentEnt"][$curIndex]  = $resRemain["REMAIN"][0];

          $_POST["gainLossEnt"][$curIndex] = $gainLoss;
          break;

        default:
          $Misc->usrDieJs("Error: Unknown Tx");
      }
      break;

    default:
      $Misc->usrDieJs("Error: Unknown Type");
  }
}

?>